home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEMP / GNU / bison / SemanticAc < prev    next >
Text File  |  1995-06-28  |  1KB  |  34 lines

  1. Semantic Actions
  2. Previous: <Semantic Values=>SemanticVa> * Next: <Bison Parser=>BisonParse> * Up: <Concepts=>Concepts>
  3.  
  4. #Wrap on
  5. {fH3}Semantic Actions{f}
  6.  
  7. In order to be useful, a program must do more than parse input; it must
  8. also produce some output based on the input.  In a Bison grammar, a grammar
  9. rule can have an {fUnderline}action{f} made up of C statements.  Each time the
  10. parser recognizes a match for that rule, the action is executed.
  11. \*Note <Actions=>Actions>.
  12.         
  13. Most of the time, the purpose of an action is to compute the semantic value
  14. of the whole construct from the semantic values of its parts.  For example,
  15. suppose we have a rule which says an expression can be the sum of two
  16. expressions.  When the parser recognizes such a sum, each of the
  17. subexpressions has a semantic value which describes how it was built up.
  18. The action for this rule should create a similar sort of value for the
  19. newly recognized larger expression.
  20.  
  21. For example, here is a rule that says an expression can be the sum of
  22. two subexpressions:
  23.  
  24. #Wrap off
  25. #fCode
  26. expr: expr '+' expr   \{ $$ = $1 + $3; \}
  27.         ;
  28. #f
  29. #Wrap on
  30.  
  31. The action says how to produce the semantic value of the sum expression
  32. from the values of the two subexpressions.
  33.  
  34.